/*
* 登陆页面
*
*
* */
'use strict';
const conn = require('../server/libs/DB/Conn');
const utils = require('../server/libs/Utils');
const { createToken } = require('../server/libs/Token');

function getFirstPath(menuList, jumpArr) {
    for (let menu of menuList) {
        if (jumpArr.length) break;
        if (menu.jump) {
            jumpArr.push(menu.jump);
            break;
        } else {
            getFirstPath(menu.children, jumpArr);
        }
    }
}

module.exports = async function (ctx) {
    let param = ctx.method === 'GET' ? ctx.query : ctx.request.fields;

    try {
        let loginName = param.loginName;
        let pwd = utils.encrypt(param.pwd);
        if (loginName && pwd) {
            let signInSql = `SELECT
                            uf.user_id,
                            uf.user_name,
                            ug.menus,
                            ug.menu,
                            ug.routes,
                            uf.domain_id,
                            uf.group_id,
                            uf.agent_id
                           FROM
                            user_file uf
                           LEFT JOIN user_group ug
                           ON ug.group_id = uf.group_id 
                           WHERE login_name = ?
                             AND PASSWORD = ?
                           AND uf.delete_sign = 1
                           AND ug.delete_sign = 1`;
            let queryResult = await conn(signInSql, [loginName, pwd]),
                userInfoObj = queryResult.shift();
            if (!userInfoObj) {
                ctx.body = { success: false, message: '登录名或密码错误!' };
            } else {
                let userInfo = {
                    user_id: userInfoObj.user_id,
                    user_name: userInfoObj.user_name,
                    group_id: userInfoObj.group_id,
                    domain_id: userInfoObj.domain_id,
                    agent_id: userInfoObj.agent_id,
                };
                let tokenInfo = { user_id: userInfo.user_id, group_id: userInfo.group_id };
                if(userInfo.group_id !== 1 && userInfo.group_id !== 3){
                    if(!userInfo.domain_id){
                      throw {  message:  '此账号无所属区域' };
                    }
                    tokenInfo.domain_id = userInfo.domain_id
                }else if(userInfo.group_id === 3){
                    if(!userInfo.agent_id){
                        throw { message: '此账号无所属代理商' };
                    }
                    tokenInfo.agent_id = userInfo.agent_id
                }
                let token = createToken(tokenInfo);   //为登陆用户创建令牌
                userInfo['menus'] = JSON.parse(userInfoObj['menu']);//从用户组表中获取用户对应的菜单和路由反馈至前台
                userInfo['routes'] = JSON.parse(userInfoObj['routes']);
                let jumpArr = [];
                getFirstPath(userInfo['menus'], jumpArr);
                userInfo['firstPath'] = jumpArr.shift();//从菜单中找到排在第一以及页面给前端作为默认页面
                ctx.body = { success: true, userInfo: userInfo, token: token };
            }
        } else {
            ctx.body = { success: false, message: '登录名或密码缺失' };
        }
    } catch (e) {
        ctx.body = { success: false, message: e.message };
    }
};
